﻿using opentel.hub.data;
using System;
using System.Collections.Concurrent;

namespace opentel.hub
{
    internal class callingstore
    {
        public ConcurrentDictionary<string, telephonist> calls = new ConcurrentDictionary<string, telephonist>();
        public ConcurrentDictionary<string, string> idmap = new ConcurrentDictionary<string, string>();

        public callingstore()
        {
        }

        public telephonist FindByCallId(string callid)
        {
            idmap.TryGetValue(callid, out var calluser);
            if (calluser == null) return null;
            calls.TryGetValue(calluser, out var call);
            return call;
        }
        public telephonist RemoveByCallId(string callid)
        {
            idmap.TryRemove(callid, out var calluser);
            if (calluser == null) return null;
            calls.TryRemove(calluser, out var call);
            return call;
        }
        public telephonist RemoveByAccount(params SIPAccountInfo[] infos)
        {
            foreach (var info in infos)
            {
                if (info == null) continue;
                calls.TryRemove(info.User, out var call);
                if (call == null) continue;
                idmap.TryRemove(call.CallID, out var callid);
                return call;
            }
            return null;
        }
        public void Insert(telephonist call)
        {
            idmap.TryAdd(call.CallID, call.Caller);
            calls.TryAdd(call.Caller, call);
        }
    }
}
